﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingAdminWebsiteManager.Espanol.Pages
{
    public partial class AdminImei : Telerik.Web.UI.RadAjaxPage
    {
        private void GetAllUser()
        {
            lbxUsers.DataSource = GPSTrackingServerDB.Data.DataRepository.UsersProvider.GetAll();
            lbxUsers.DataBind();
        }

        private void GetAllImeiWithinUsers()
        {
            lbxImeis.DataSource = GPSTrackingServerDBCustom.Data.DataRepository.ImeiProvider.GetAllImeiWithinUser();
            lbxImeis.DataBind();
        }

        private void GetGroupsByUser(string Username)
        {
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.UsersByGroups> usersByGroupsEntities = GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.GetByUsername(Username);

            GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.DeepLoad(usersByGroupsEntities, true, GPSTrackingServerDB.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingServerDB.Entities.Groups));

            //Parse
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Groups> groupsEntities = new GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Groups>();
            foreach (GPSTrackingServerDB.Entities.UsersByGroups entity in usersByGroupsEntities)
            {
                groupsEntities.Add(entity.GroupIdSource);
            }

            lbxGroups.DataSource = groupsEntities;
            lbxGroups.DataBind();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetAllUser();

                GetAllImeiWithinUsers();                
            }
        }

        protected void lbxUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbxImeis.Enabled = true;

            lbxGroups.Enabled = true;

            GetGroupsByUser(lbxUsers.SelectedValue);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (lbxImeis.CheckedItems.Count > 0)
            {
                GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();
                transactionManager.BeginTransaction();

                foreach (Telerik.Web.UI.RadListBoxItem item in lbxImeis.CheckedItems)
                {
                    //Imei
                    GPSTrackingServerDB.Entities.Imei imeiEntity = new GPSTrackingServerDB.Entities.Imei();
                    imeiEntity.Imei = item.Value;
                    imeiEntity.Name = "Sin Nombre";
                    imeiEntity.Description = "Sin Nombre";
                    imeiEntity.SimNumber = 0;
                    imeiEntity.GpsModelId = 1;
                    imeiEntity.ImeiTypeId = 1;

                    GPSTrackingServerDB.Data.DataRepository.ImeiProvider.Insert(transactionManager, imeiEntity);

                    //Group by imei
                    GPSTrackingServerDB.Entities.GroupsByImei groupsByImeiEntity = new GPSTrackingServerDB.Entities.GroupsByImei();
                    groupsByImeiEntity.Imei = item.Value;
                    groupsByImeiEntity.GroupId = Convert.ToInt32(lbxGroups.SelectedValue);

                    GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Insert(transactionManager, groupsByImeiEntity);                    
                }

                transactionManager.Commit();

                //refresh
                GetAllImeiWithinUsers();

                RadNotification1.Show();
            }
        }
    }
}